using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using TnPlus.Entities.Models;
using TnPlus.Entities.Utils;
using TnPlus.Common;

namespace TnPlus.DAL.Base
{
	public class FactorCustomersHistoryDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public FactorCustomersHistoryDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the FactorCustomersHistory table.
		/// </summary>
		public virtual void Insert(FactorCustomersHistoryInfo factorCustomersHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdFactorCustomer", factorCustomersHistoryInfo.IdFactorCustomer),
				new SqlParameter("@PostDate", factorCustomersHistoryInfo.PostDate),
				new SqlParameter("@IdCommon", factorCustomersHistoryInfo.IdCommon),
				new SqlParameter("@AccountNo", factorCustomersHistoryInfo.AccountNo),
				new SqlParameter("@IMCustomer", factorCustomersHistoryInfo.IMCustomer),
				new SqlParameter("@AdjustRate", factorCustomersHistoryInfo.AdjustRate)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "FactorCustomersHistory_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the FactorCustomersHistory table.
		/// </summary>
		public virtual void Update(FactorCustomersHistoryInfo factorCustomersHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdFactorCustomer", factorCustomersHistoryInfo.IdFactorCustomer),
				new SqlParameter("@PostDate", factorCustomersHistoryInfo.PostDate),
				new SqlParameter("@IdCommon", factorCustomersHistoryInfo.IdCommon),
				new SqlParameter("@AccountNo", factorCustomersHistoryInfo.AccountNo),
				new SqlParameter("@IMCustomer", factorCustomersHistoryInfo.IMCustomer),
				new SqlParameter("@AdjustRate", factorCustomersHistoryInfo.AdjustRate)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "FactorCustomersHistory_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the FactorCustomersHistory table by its primary key.
		/// </summary>
		public virtual void Delete(Guid? idFactorCustomer)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdFactorCustomer", idFactorCustomer)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "FactorCustomersHistory_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the FactorCustomersHistory table.
		/// </summary>
		public virtual FactorCustomersHistoryInfo Select(Guid? idFactorCustomer)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdFactorCustomer", idFactorCustomer)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "FactorCustomersHistory_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeFactorCustomersHistoryInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the FactorCustomersHistory table.
		/// </summary>
		public List<FactorCustomersHistoryInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "FactorCustomersHistory_SelectAll"))
			{
				List<FactorCustomersHistoryInfo> factorCustomersHistoryInfoList = new List<FactorCustomersHistoryInfo>();
				while (dataReader.Read())
				{
					factorCustomersHistoryInfoList.Add(MakeFactorCustomersHistoryInfo(dataReader));
				}

				return factorCustomersHistoryInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the FactorCustomersHistory class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual FactorCustomersHistoryInfo MakeFactorCustomersHistoryInfo(SqlDataReader dataReader)
		{
			FactorCustomersHistoryInfo factorCustomersHistoryInfo = new FactorCustomersHistoryInfo();
			factorCustomersHistoryInfo.IdFactorCustomer = SqlClientUtility.GetGuid(dataReader, DbConstants.FACTORCUSTOMERSHISTORY.IDFACTORCUSTOMER);
			factorCustomersHistoryInfo.PostDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.FACTORCUSTOMERSHISTORY.POSTDATE);
			factorCustomersHistoryInfo.IdCommon = SqlClientUtility.GetString(dataReader, DbConstants.FACTORCUSTOMERSHISTORY.IDCOMMON);
			factorCustomersHistoryInfo.AccountNo = SqlClientUtility.GetString(dataReader, DbConstants.FACTORCUSTOMERSHISTORY.ACCOUNTNO);
			factorCustomersHistoryInfo.IMCustomer = SqlClientUtility.GetDecimal(dataReader, DbConstants.FACTORCUSTOMERSHISTORY.IMCUSTOMER);
			factorCustomersHistoryInfo.AdjustRate = SqlClientUtility.GetDecimal(dataReader, DbConstants.FACTORCUSTOMERSHISTORY.ADJUSTRATE);

			return factorCustomersHistoryInfo;
		}

		#endregion
	}
}
